/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/8/12 9:17
 * @注释:线程安全问题
 * 加锁synchronized 解决了'脏读问题'
 */
class Demo{
    public int count = 0;
    public synchronized void add(){
        for (int i = 0; i < 50000; i++) {
            count++;
        }
    }
}
public class ThreadDemo12 {
    public static void main(String[] args) {
        Demo d = new Demo();
        Thread t1 = new Thread(()->{
            d.add();
        }) ;

        Thread t2 = new Thread(()->{
            d.add();
        }) ;

        t1.start();
        t2.start();

        try {
            t2.join();
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(d.count);
    }
}
